﻿<%@ WebHandler Language="C#" Class="OrderGo" %>

using System;
using System.Web;

public class OrderGo : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        string tab = context.Request.QueryString["tab"];
        string xml = context.Request.QueryString["xml"];
        string direction = context.Request.QueryString["d"];
        string id = context.Request.QueryString["id"];

        ConfigTable cTab = ConfigTable.GetConfigTable(tab, xml);

        string groupCol = cTab.OrderGroupColumnName;

        string groupSql = string.Empty;
        if (!string.IsNullOrEmpty(groupCol))
        {
            groupSql = " and " + groupCol + "=(select "+groupCol+" from "+tab+" where "+cTab.PkFiledName+"="+DataTypeFactory.GetSqlValue(id,cTab[cTab.PkFiledName].ColumnDataType)+")";
        }

        bool descOrder = false;

        string orderSqlString = cTab.OrderSql.ToLower();
        int orderColinOrderSqlIndex = orderSqlString.IndexOf(cTab.OrderFieldName);
        if (orderColinOrderSqlIndex > -1)
        {
            string afterOrderCol = orderSqlString.Substring(orderColinOrderSqlIndex + cTab.OrderFieldName.Length, cTab.OrderSql.Length - orderColinOrderSqlIndex - cTab.OrderFieldName.Length);
            if (afterOrderCol.ToLower().StartsWith(" desc"))
            {
                descOrder = true;
            }
        }

        string sng = string.Empty;
        string sngOrder = string.Empty;
        //如果是倒序
        if (descOrder)
        {
            //如果是向上
            if (direction == "up")
            {
                sng = ">";
                sngOrder = " order by id";
            }
            //向下
            else
            {
                sng = "<";
                sngOrder = " order by id desc";
            }
        }
        //不是倒序
        else
        {
            //如果是向上
            if (direction == "up")
            {
                sng = "<";
                sngOrder = " order by id desc";
            }
            //向下
            else
            {
                sng = ">";                
            }
        }

        string selectThisOrderSql = "select " + cTab.OrderFieldName + " from " + tab + " as b where b." + cTab.PkFiledName + "=" + DataTypeFactory.GetSqlValue(id, cTab[cTab.PkFiledName].ColumnDataType);
        context.Response.Write(selectThisOrderSql + "<br>");
        string thisOrderValue = DBFactory.GetConn().exe1(selectThisOrderSql);

        string nextSql = "select "+cTab.PkFiledName+","+cTab.OrderFieldName+" from " + tab + " as a where " + cTab.OrderFieldName + sng +thisOrderValue+groupSql+sngOrder;

        context.Response.Write(nextSql + "<br>");
        
        System.Data.DataRow row = DBFactory.GetConn().exeRow(nextSql);
        if (row != null)
        {
            string nextOrderValue = row[cTab.OrderFieldName].ToString();
            string nextPkValue = row[cTab.PkFiledName].ToString();

            string updateSql = "update " + tab + " set " + cTab.OrderFieldName + "=" + DataTypeFactory.GetSqlValue(nextOrderValue, cTab[cTab.OrderFieldName].ColumnDataType) + " where " + cTab.PkFiledName + "=" + DataTypeFactory.GetSqlValue(id, cTab[cTab.PkFiledName].ColumnDataType);

            string updateSql1 = "update " + tab + " set " + cTab.OrderFieldName + "=" + DataTypeFactory.GetSqlValue(thisOrderValue, cTab[cTab.OrderFieldName].ColumnDataType) + " where " + cTab.PkFiledName + "=" + DataTypeFactory.GetSqlValue(nextPkValue, cTab[cTab.PkFiledName].ColumnDataType);

            DBFactory.GetConn().exe0(updateSql);
            DBFactory.GetConn().exe0(updateSql1);

            context.Response.Write(updateSql+"<br>");
            context.Response.Write(updateSql1);
        }

        context.Response.Redirect(context.Request.UrlReferrer.ToString());
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}